package fdoj.eventInvitations.servlet;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import fdoj.eventInvitations.dao.ContactDao;
import fdoj.eventInvitations.entity.Contact;
import fdoj.eventInvitations.entity.Person;

public class ChangeContactController extends HttpServlet{
	@EJB
	private ContactDao contactDao;
	private HttpSession session;
	private Person loggedUser;
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {

			session = request.getSession();
			loggedUser = (Person) session.getAttribute("loggedUser");
			if(loggedUser == null){
				response.sendRedirect(response.encodeRedirectURL("./index"));
				return;
			}
			String firstName = request.getParameter("firstName");
			String lastName = request.getParameter("lastName");
			String email = request.getParameter("email");
			Contact c= (Contact) request.getSession().getAttribute("editContact");
			c.setEmail(email);
			c.setFirstName(firstName);
			c.setLastName(lastName);
			contactDao.merge(c);
			
			response.sendRedirect(response.encodeRedirectURL("./addressbook"));
		    return;

		} catch (Exception e) {
			e.printStackTrace();

		}
		response.sendRedirect(response.encodeRedirectURL("./error.jsp"));
	}
}
